package com.xbaoziplus.simple_spring.core.io;

import cn.hutool.core.lang.Assert;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author xbaozi
 * @version 1.0
 * @classname DefaultResourceLoader
 * @date 2023-05-07  13:51
 * @description 对三种不同类型的资源处理方式进行包装
 */
public class DefaultResourceLoader implements ResourceLoader {
    @Override
    public Resource getResource(String location) {
        Assert.notNull(location, "Location must not be null");
        if (location.startsWith(CLASSPATH_URL_PREFIX)) {
            return new ClassPathResource(
                    location.substring(CLASSPATH_URL_PREFIX.length())
            );
        } else {
            try {
                URL url = new URL(location);
                return new UrlResource(url);
            } catch (MalformedURLException e) {
                return new FileSystemResource(location);
            }
        }
    }
}
